package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ConsultationAdapter extends BaseAdapter{

	private static final String TAG = "ConsultationAdapter";
	
	//private ImageSwitcher mImageLoader;
	private int mCount;
	private Context mContext;
	private JSONArray consultationJsonArray =null;
	
	public ConsultationAdapter(int count, Context context, JSONArray consultationJsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.consultationJsonArray = consultationJsonArray;
		//mImageLoader = new ImageSwitcher(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		if(convertView==null)
		{
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_consultation, null);
			viewholder = new ViewHolder();
			viewholder.tv_xm = (TextView)convertView.findViewById(R.id.tv_xm);
			viewholder.tv_kdkb = (TextView)convertView.findViewById(R.id.tv_kdkb);
			viewholder.tv_kdys = (TextView)convertView.findViewById(R.id.tv_kdys);
			viewholder.tv_hzkb = (TextView)convertView.findViewById(R.id.tv_hzkb);
			viewholder.tv_hzys = (TextView)convertView.findViewById(R.id.tv_hzys);
			viewholder.tv_zt = (TextView)convertView.findViewById(R.id.tv_zt);
			convertView.setTag(viewholder);
		}
		else
		{
			viewholder = (ViewHolder)convertView.getTag();
		}
			try {
				JSONObject patients = consultationJsonArray.getJSONObject(position);
				if(patients!=null)
				{
					viewholder.tv_xm.setText(patients.getString("PNAME"));
					viewholder.tv_kdkb.setText(patients.getString("QQKNAME"));
					viewholder.tv_kdys.setText(patients.getString("QQDNAME"));
					viewholder.tv_hzkb.setText(patients.getString("DFKNAME"));
					viewholder.tv_hzys.setText(patients.getString("DFDNAME"));
					viewholder.tv_zt.setText(patients.getString("STATENAME"));
				}
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(TAG, e.toString());
			}
		return convertView;
	}
	
	static class ViewHolder {
		TextView tv_xm;
		TextView tv_kdkb;
		TextView tv_kdys;
		TextView tv_hzkb;
		TextView tv_hzys;		
		TextView tv_zt;
	}
}